package com.xinchao.ck_mips.widgets;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.xinchao.ck_mips.base.CKSubscriber;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class InfoDialog extends Dialog {
    //记录弹窗开启时间
    //private long time;
    //private boolean isShow;

    public InfoDialog(Context context, int theme) {
        super(context, theme);
        //isShow = true;
        //time = System.currentTimeMillis();
        //closeDialog();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            dismiss();
        }
        return false;
    }

    /**
     * 重新dismiss，弹窗消失，隐藏输入键盘
     */
    @Override
    public void dismiss() {
        //isShow = false;
       // time = 0;
        View view = getCurrentFocus();
        if (view instanceof EditText) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
        }

        super.dismiss();
    }


    /*private void closeDialog() {
        Flowable.timer(5, TimeUnit.SECONDS, Schedulers.io()).subscribe(new CKSubscriber<Long>(null) {
            @Override
            protected void next(Long aLong) {
                if (isShow && System.currentTimeMillis() - time > 30000) {
                    dismiss();
                }
            }

            @Override
            protected void finish() {
                if (isShow) {
                    closeDialog();
                }
                super.finish();
            }
        });
    }*/

}
